package com.wjbgn.electronicnian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianUserDO;
import com.wjbgn.electronicnian.dto.NianUserDTO;
import com.wjbgn.electronicnian.service.NianUserService;
import com.wjbgn.electronicnian.convert.NianUserDoConvert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 
 * Create Date: 2022-01-20T11:06:12.883426600
 * @author weirx
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class NianUserController {

    /**
     * NianUserService
     */
    private NianUserService nianUserService;

    /**
     * 分页列表
     * @param nianUserDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(NianUserDTO nianUserDTO) {
        QueryWrapper<NianUserDO> queryWrapper = new QueryWrapper<>(NianUserDoConvert.dtoToDo(nianUserDTO));
        Page<NianUserDO> page = new Page<>(nianUserDTO.getPage(), nianUserDTO.getLimit());
        Page<NianUserDO> pageList = nianUserService.page(page, queryWrapper);
        return Result.success(NianUserDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     * @param nianUserDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(NianUserDTO nianUserDTO) {
        QueryWrapper<NianUserDO> queryWrapper = new QueryWrapper<>(NianUserDoConvert.dtoToDo(nianUserDTO));
        List<NianUserDO> nianUserList = nianUserService.list(queryWrapper);
        return Result.success(NianUserDoConvert.listConvert(nianUserList));
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public Result info(@RequestParam("id")Long id) {
            NianUserDO nianUser = nianUserService.getById(id);
        return Result.success(null == nianUser ? null : NianUserDoConvert.doToDto(nianUser));
    }

    /**
     * 新增
     * @param nianUserDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody NianUserDTO nianUserDTO) {
        boolean flag = nianUserService.save(NianUserDoConvert.dtoToDo(nianUserDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 更新
     * @param nianUserDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody NianUserDTO nianUserDTO) {
        boolean flag = nianUserService.updateById(NianUserDoConvert.dtoToDo(nianUserDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }


    /**
     * 登录并注册
     * @param nianUserDTO
     * @return
     */
    @ApiOperation(value = "login")
    @PostMapping("/login")
    public Result login(@RequestBody NianUserDTO nianUserDTO) {
        return nianUserService.login(NianUserDoConvert.dtoToDo(nianUserDTO));
    }
}